// pages/solitaire/solitaire.js
var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
      inpvalue:'',
      gtime:'',
      uid:'',

      light1:'nav1',
      light2:'unnav2',
      hide1:'user1',
      hide2:'unuser2'
  },
  getCommentData:function(){
    let self = this;
    wx.request({
      url:'https://wx.original5.xyz:2003/game/game1', 
      // data: {
      //   detailnum:self.data.detailnum
      // },
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        let result = res.data;
        if (result){
          result.sort(function(a,b){
            return a.gtime > b.gtime ? 1 : -1
          }),
          self.setData({
            
            commentDetail:result.reverse()
          })
        }
      }
    })
  },

  bindinp:function(e){
    this.data.inpvalue = e.detail.value;
  },
  subclick:function(){
    var that = this;
    var inpvalue=this.data.inpvalue
    console.log(inpvalue)
    //获取当前时间
    var dayTime = util.formatTime(new Date());
    console.log(dayTime)
    var time1 = dayTime.split('/')
    var time2 = time1[0]+'-'+time1[1]+'-'+time1[2]+' '
    var time3 = time2.split(':')
    var time4 = time3[0]+':'+time3[1]
    console.log(time4)
    that.setData({
      gtime:time4
    })
    


    if(wx.getStorageSync('uid')){  //已登录
      console.log("uid:",wx.getStorageSync('uid'));

      this.setData({
          Uimage:wx.getStorageSync('Uimage'),
          Uname:wx.getStorageSync('Uname'),
          uid:wx.getStorageSync('uid'),
          isLogin:true
      }) 

      if(inpvalue===''){
        wx.showModal({
          title:'提示',
          content:'你还没有填写内容哦~',
          success: function(res){
            if(res.confirm){
              console.log('点击确定了')
            }else if(res.cancel){
              console.log('点击取消了')
              return false;
            }
          }
        })
      }else{
        wx.redirectTo({
          url: '/pages/solitaire/solitaire',
        })
        wx.request({
          url:'https://wx.original5.xyz:2003/game/game1',
          data:{
            uid:that.data.uid,
            gcontent:inpvalue,
            gtime:that.data.gtime
          },
          method:'post',
          header:{
            'content-type':'application/json'
          },
          
          fail:function(res){
            console.log('发布失败！')
          }
        })
      }
  }
  else{

    console.log('未登录')
    wx.showModal({
      title: '提示',
      content: '登录后收藏',
      showCancel: true, 
      cancelText: "不登录", 
      cancelColor: ' rgb(178, 89, 75)', 
      confirmText: "去登录", 
      confirmColor: 'rgb(178, 89, 75)', 
      success: function(res) {
        if (res.cancel) {
       
         
        } else if(res.confirm){
          
          wx.navigateTo({
            url: '../../pages/User/User',
          })
        }
      }
    })
  }
  },

  firstclick: function () {
    this.setData({
      light1:'nav1',
      light2:'unnav2',
      hide1:'user1',
      hide2:'unuser2'
    })
    this.getCommentData()
  },
  secondclick: function () {
    this.setData({
      light1:'unnav1',
      light2:'nav2',
      hide1:'unuser1',
      hide2:'user2'
    })
    this.getScore()
  },
  getScore: function(){
    let self = this;
    wx.request({
      url:'https://wx.original5.xyz:2003/game/game1/score', 
      method:'GET',
      header: {
        'content-type': 'application/json' 
      },
      success: function (res) {
        console.log(res.data)
        
        let result = res.data;
        for (var i = 0; i < result.length; i++) {
          result[i].len = result[i].len*9
          
        }
        
        let arr2 = result.sort(function(a,b){
          return b.len-a.len
        })
        for (var j = 0; j < arr2.length; j++) {
          result[j].position = j+1 
        }
      console.log(arr2)
        if (arr2){
          self.setData({
            score:arr2
          })
        }
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCommentData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})